import Vue from 'vue'
import Vuex from 'vuex'
import storage from 'store'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userType: '',
    userInfo: {},
    token: ''
  },
  mutations: {
    // 设置页面type类型
    SET_USER_TYPE (state, payload) {
      state.userType = payload
      storage.set('userType', payload)
    },
    // 设置token
    SET_TOKEN: (state, token) => {
      state.token = token
      storage.set('token', token)
    },
    // 设置用户信息
    SET_USER_INFO: (state, info) => {
      state.userInfo = info
      storage.set('userInfo', info)
    }
  },
  getters: {
    token: state => {
      let token = state.token
      if (!token) {
        token = storage.get('token') || null
      }
      return token
    },
    userInfo: state => {
      let userInfo = state.userInfo
      if (Object.keys(userInfo).length === 0) {
        userInfo = storage.get('userInfo') || {}
      }
      return userInfo
    },
    userType: state => {
      let userType = state.userType
      if (!userType) {
        userType = storage.get('userType') || '1'
      }
      return userType
    }
  },
  actions: {},
  modules: {
  }
})
